﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WebKit.DOM;
using WebKit;
using Crawler_v1.utils;
using Crawler_v1.bean;
using Newtonsoft.Json;

namespace Crawler_v1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webKitBrowser1.Navigate("http://fec.mofcom.gov.cn/article/fwydyl/");

            webKitBrowser1.Navigated +=
       new WebBrowserNavigatedEventHandler(webKitBrowser1_Navigated);

            webKitBrowser1.Error += new WebKitBrowserErrorEventHandler(webKitBrowser1_Error);
        }

        void webKitBrowser1_Error(Object sender, WebKitBrowserErrorEventArgs e)
        {
            Console.WriteLine(e.Description);
        }


        void webKitBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            //
            string str = webKitBrowser1.StringByEvaluatingJavaScriptFromString("window.location.href");
            Console.WriteLine("str:" + str);

            //

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Document document = webKitBrowser1.Document;
            var head = document.GetElementsByTagName("head");

            var script = document.CreateElement("script");
            script.SetAttribute("type", "text/javascript");
            script.SetAttribute("src", "");
            script.TextContent = string.Format(";{0};", FileUtil.GetScript("crawler.js"));//"jquery.js"

            head[0].AppendChild(script);

        }

        private void button2_Click(object sender, EventArgs e)
        {

            //var result=webKitBrowser1.Document.InvokeScriptMethod(textBox1.Text);

            //"$('body').height();"
            
            try
            {
                string str = webKitBrowser1.StringByEvaluatingJavaScriptFromString(textBox1.Text);
                Console.WriteLine("str2:" + str);
                label1.Text = "result:" + str;
                
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string str = webKitBrowser1.Document.InvokeScriptMethod("getList", new object[] { }) as string;
                Console.WriteLine("obj:" + str);
                //label1.Text = "result:" + str;
                label1.Text=JsonConvert.DeserializeObject<List<ListBean>>(str).ToString();
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }

            /*
            string func = "(function(){" + textBox1.Text + "})()";

            try
            {
                var result = webKitBrowser1.Document.InvokeScriptMethod(func);
                label1.Text = "result:" + result;
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }
            */
        }

        private void getList()
        {
            var dls = webKitBrowser1.Document.GetElementsByTagName("dl");
            foreach (var dl1 in dls)
            {

            }
        }
    }
}
